home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 34.zip / BS1 part 34 / FredFish PD 319.adf / CNewsSrc / uupc.lzh / uupc / sio.c < prev    next >
C/C++ Source or Header  |  1990-01-16  |  5KB  |  223 lines

  1. /*
  2.  *    Sio.c
  3.  *    Created: 27Jun87 by J.A. Lydiatt
  4.  *
  5.  *    Amiga Serial I/O Library
  6.  *
  7.  *    $Id: sio.c,v 1.2 90/01/16 10:27:26 crash Exp Locker: crash $
  8.  */
  9.  
  10. #ifndef lint
  11. static char RCSid[] = "$Id: sio.c,v 1.2 90/01/16 10:27:26 crash Exp Locker: crash $";
  12. #endif /* lint */
  13.  
  14. #include <stdio.h>
  15. #ifndef TRUE
  16. #  define TRUE -1
  17. #  define FALSE 0
  18. #endif
  19.  
  20. /*--- Timer Function Declarations --- */
  21.  
  22. extern void CloseTimer();    /* Return Timer's resources to the System */
  23. extern int  OpenTimer();     /* Returns True if Timer Device Opened OK */ 
  24. extern void StartTimer();    /* (long)seconds, (long)microseconds      */
  25. extern int  TimerExpired();     /* Returns True if Timer interval expired */
  26.  
  27. /*--- SerialIO Function Declarations --- */
  28.  
  29. extern struct EOExtSer *InitSerialIO(); /* problems? return NULL      */
  30. extern void CloseSerialIO();        /* Return SerialIO resources      */
  31. extern int  CheckSerIO();        /* TRUE if character available    */
  32. extern char SerIORead();        /* Returns a Char from serial port*/
  33. extern void SerIOWrite();         /* Writes one byte to serial port */ 
  34. extern void SetSerBaud();        /* (int)baud - set baud rate      */
  35.  
  36. /*--------------------------------------------------------------*/
  37. /*     getspeed/getbaud: check for valid baud parameters    */
  38. /*--------------------------------------------------------------*/
  39.  
  40. static struct {
  41.     unsigned baudr;
  42.     int speedcode;
  43.  
  44. } speeds[] = {
  45.     300,    300,
  46.     1200,    1200,
  47.     2400,    2400,
  48.     4800,    4800,
  49.     9600,    9600,
  50.     19200, 19200,
  51.     0,
  52. };
  53.  
  54. static unsigned getspeed(code)
  55. {
  56.     register n;
  57.  
  58.     for (n=0; speeds[n].baudr; ++n)
  59.         if (speeds[n].speedcode == code)
  60.             return speeds[n].baudr;
  61.     return 0;
  62. }
  63.  
  64.  
  65. static unsigned getbaud(code)
  66. char *code;
  67. {
  68.     register n;
  69.     register int Baudrate;
  70.     
  71.     Baudrate = atoi(code);
  72.     
  73.     for (n=0; speeds[n].baudr; ++n)
  74.         if (speeds[n].baudr == Baudrate)
  75.             return speeds[n].speedcode;
  76.     return 0;
  77. }
  78.  
  79. void ssendbrk(cnt)
  80. int cnt;
  81. {
  82.     SerIOBreak(cnt * 250000L);
  83. }
  84.  
  85. /*--------------------------------------------------------------*/
  86. /*    SIOInit: initialize serial I/O                */
  87. /*--------------------------------------------------------------*/
  88.  
  89.  
  90. SIOInit ( whichport, speed )
  91. char * whichport;
  92. char * speed;
  93. {
  94.     unsigned int baud;
  95.     extern void SetXonMode();
  96.  
  97.     /*  fprintf( stderr, "sioinit %s %s\n", whichport, speed ); /* */
  98.  
  99.     if ( InitSerialIO() == NULL ) {
  100.         fprintf( stderr, "Can't open Serial Port\n" );
  101.         return( -1 );
  102.     }
  103.  
  104.      if ( baud = getbaud( speed ) )
  105.     SetSerBaud( baud );
  106.  
  107.      if ( !OpenTimer() )
  108.     {
  109.        CloseSerialIO();
  110.        fprintf( stderr, "Can't open the Timer Device\n" );
  111.        return -1;
  112.         }
  113.  
  114.      SetXonMode( (int)FALSE );
  115.      return( 0 );
  116. }
  117.  
  118. /*--------------------------------------------------------------*/
  119. /*    SIOSpeed:  set the serial Baud rate             */
  120. /*--------------------------------------------------------------*/
  121.  
  122. SIOSpeed( speed )
  123. char *speed;
  124. {
  125.     unsigned int baud;
  126.  
  127.     if (baud = getbaud( speed ))
  128.         SetSerBaud( baud );
  129. }
  130.  
  131. /*--------------------------------------------------------------*/
  132. /*    SIOInBuffer/ SIOOutBuffer:    Set Buffer Size.    */
  133. /*--------------------------------------------------------------*/
  134.  
  135. SIOInBuffer ( buf, size )
  136. char * buf;
  137. int size;
  138. {
  139. }
  140.  
  141. SIOOutBuffer ( buf, size )
  142. char * buf;
  143. int size;
  144. {
  145. }
  146.  
  147. /*--------------------------------------------------------------*/
  148. /*    SIOClose:    Close Serial port.            */
  149. /*--------------------------------------------------------------*/
  150.  
  151. SIOClose ( dtr )
  152. {
  153.    CloseSerialIO();
  154.    CloseTimer();
  155. }
  156.  
  157. /*--------------------------------------------------------------*/
  158. /*    SIOWrite: send count characters to serial port        */
  159. /*--------------------------------------------------------------*/
  160.  
  161. SIOWrite ( buf, count )
  162. register char * buf;
  163. int count;
  164. {
  165.     register int nsent;
  166.     char *prt();
  167.  
  168.     nsent = count;
  169.     while ( nsent-- )
  170.        SerIOWrite( *buf++ );
  171.  
  172.     return count;
  173. }
  174.  
  175. /*--------------------------------------------------------------*/
  176. /*    SIORead: read mincount <= #characters <= maxcount    */
  177. /*    return  # of characters read, or -1 if timed out after    */
  178. /*    after tenths / 10 seconds.                */
  179. /*--------------------------------------------------------------*/
  180.  
  181.  
  182. int SIORead ( buf, mincount, maxcount, tenths )
  183. register char *buf;
  184. int    mincount;
  185. int    maxcount;
  186. int    tenths; /* timeout is in tenth's of seconds */
  187. {
  188.         /* A bit of a Kludge to get the project started */
  189.  
  190.     long seconds, microSeconds;
  191.     unsigned long waitMask;
  192.     int  timerON ;
  193.     register int received;
  194.  
  195. #if 0
  196.     fprintf (stderr, "Read  m= %d T= %ld", maxcount, Ticks  );
  197. #endif
  198.     if ( tenths == 0 ) {
  199.         if (!CheckSerIO()) return -1;
  200.         tenths = 10;
  201.     }
  202.     seconds = tenths / 10;
  203.     microSeconds = ((long)(tenths) - seconds * 10L) * 100000L;
  204.  
  205.     received = 0;
  206.     waitMask = (1L << GetSerialSigBit()) | (1L << GetTimerSigBit() ); 
  207.     timerON = FALSE;
  208.     do {
  209.         if ( CheckSerIO() ) {
  210.             *buf++ = SerIORead();
  211.             received++;
  212.             if ( mincount <= received && received <= maxcount )
  213.                 return received;
  214.         }
  215.         if ( !timerON ) {
  216.             StartTimer( seconds, microSeconds );
  217.             timerON = TRUE;
  218.         }
  219.         Wait( waitMask );
  220.     } while ( !TimerExpired() );
  221.     return -1;
  222. }
  223.